﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Webcar.BLL;
using ToolsLayer;
using BusinessEntity;

namespace Webcar.Car
{
    public partial class ChildDeptList :PageBase
    {
        private string dept_code
        {
            get
            {
                return Request.QueryString["dept_code"];
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            GridViewStyler gvs = new GridViewStyler(this.GridView1);
            gvs.SetStyle();

            if (this.IsPostBack)
                return;
            DropDownListManager.BindDropDownDW(this.DropDownList1);
            if (!string.IsNullOrEmpty(dept_code))
                this.DropDownList1.SelectedValue = dept_code;
            this.BindList();
        }

        protected void BindList()
        {
            if (!string.IsNullOrEmpty(this.DropDownList1.SelectedValue))
            {
                string sql = @"select *,case ISNULL(is_use,'T') when 'T' then '正常' else '已停用' end is_use_name from DEPT_CHILD
                     where up_dept = '"+this.DropDownList1.SelectedValue+"'";
                this.GridView1.DataSource = DB.ExecuteDataset(sql);
                this.GridView1.DataBind();
            }
        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            Response.Redirect("ChildDeptList.aspx?dept_code=" + this.DropDownList1.SelectedValue);
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            string updept = this.DropDownList1.SelectedValue;
            if (string.IsNullOrEmpty(updept))
            {
                this.lblError.Text = "请先选择上级单位。";
                return;
            }
            if (string.IsNullOrEmpty(this.tbDeptName.Text ))
            {
                this.lblError.Text = "请输入下级单位名称。";
                return;
            }
            DEPT_CHILDEntity dc = new DEPT_CHILDEntity();
            dc.UP_DEPT = updept;
            dc.DEPT_NAME = this.tbDeptName.Text;
            dc.CHILD_DEPT = SerialNoManager.GetChildDept();
            dc.Save();
            Response.Redirect("ChildDeptList.aspx?dept_code="+this.DropDownList1.SelectedValue);
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowIndex == -1)
                return;
            e.Row.Cells[0].Text = (e.Row.RowIndex + 1).ToString();
            LinkButton btn = (LinkButton)e.Row.FindControl("lnkStop");
            if (btn == null) return;
            DataRowView row = e.Row.DataItem as DataRowView;
            if (row["is_use"].ToString() == "F")
            {
                btn.Text = "启用";

                btn.Attributes.Add("onclick", "return confirm('确定要启用该单位吗？');");
            }
            else
            {

                btn.Attributes.Add("onclick", "return confirm('确定要停用该单位吗？');");
            }
        }
        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Hide")
            {
                string childid = e.CommandArgument.ToString();
                string s = DB.ExecuteScalar("select count(1) from bs_car_record where child_dept='" + childid + "' and is_use='T'");
                if (s != "0")
                {
                    this.AjaxAlert(this.UpdatePanel1, "该下级单位下有车辆档案，不能被停用。");
                    //this.ScriptManager1.RegisterClientScriptBlock("该下级单位下有车辆档案，不能被停用。");
                    return;
                }

                LinkButton btn = e.CommandSource as LinkButton;
                string flag = "F";
                if (btn.Text == "启用")
                {
                    flag = "T";
                }
                
                string sql = "update dept_child set is_use='"+flag+"' where child_dept='"+childid+"'";
                DB.ExecuteNonQuery(sql);
                this.BindList();
            }
        }
        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            this.GridView1.EditIndex = e.NewEditIndex;
            this.BindList();
        }
        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            string keyid = GridView1.DataKeys[e.RowIndex].Values[0].ToString();
            DEPT_CHILDEntity dc = new DEPT_CHILDEntity();
            dc.CHILD_DEPT = keyid;
            //dc.UP_DEPT = keyid;
            dc.Retrieve();
            dc.DEPT_NAME = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox2")).Text;
            dc.Save();
            Response.Redirect("ChildDeptList.aspx?dept_code=" + this.DropDownList1.SelectedValue);
        }
        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            string keyid = GridView1.DataKeys[e.RowIndex].Values[0].ToString();
            string sql = "delete from dept_child where child_dept='" + keyid + "'";
            string s = DB.ExecuteScalar("select count(1) from bs_car_record where child_dept='" + keyid + "'");
            if (s != "0")
            {
                this.AjaxAlert(this.UpdatePanel1, "该下级单位下有车辆档案，不能被删除。");
                return;
            }

            s = DB.ExecuteScalar("select count(1) from sec_record where child_dept='" + keyid + "'");
            if (s != "0")
            {
                this.AjaxAlert(this.UpdatePanel1, "该下级单位下已经有安全事故档案，不能被删除。");
                return;
            }

            s = DB.ExecuteScalar("select count(1) from rp_car_repair_head where child_dept='" + keyid + "'");
            if (s != "0")
            {
                this.AjaxAlert(this.UpdatePanel1, "该下级单位下已经有维修记录档案，不能被删除。");
                return;
            }

            s = DB.ExecuteScalar("select count(1) from bs_emp where child_dept='" + keyid + "'");
            if (s != "0")
            {
                this.AjaxAlert(this.UpdatePanel1, "该下级单位下已经有驾驶员档案，不能被删除。");
                return;
            }

            DB.ExecuteNonQuery(sql);
            Response.Redirect("ChildDeptList.aspx?dept_code=" + this.DropDownList1.SelectedValue);
        }
        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            this.GridView1.EditIndex = -1;
            this.BindList();
        }
    }
}
